/**
 * 定义应用路由
 */
import React from 'react'
import { hashHistory, Router, } from 'react-router'
import $ from 'jquery'
// import {
//   // BrowserRouter as Router,
//   // Route,
//   // Link,
//   Redirect,
//   // withRouter
// } from "react-router-dom";
// 路由配置规则参考： https://github.com/ReactTraining/react-router/blob/v3/docs/guides/RouteConfiguration.md#configuration-with-plain-routes
// 一下部分是由 ICE 相关工具自动生成的路由，请勿随意改变，否则可能会出现一些异常情况
// <!-- auto generated routes start -->
import HeaderAsideFooterResponsiveLayout from './layouts/HeaderAsideFooterResponsiveLayout'
import Api from './pages/Api'
import NotFound from './pages/NotFound'
import Black from './pages/Black'
import Home from './pages/Home'

/**
 * 左侧菜单收缩切换
 */

const autoGeneratedRoutes = [{
  path: "/api",
  component: HeaderAsideFooterResponsiveLayout,
  indexRoute: {
    component: Home
  }
}, {
  path: "/api/home",
  component: HeaderAsideFooterResponsiveLayout,
  indexRoute: {
    component: Home
  }
}]; //调用后台接口生成路由

$.ajax({
  url: '/api/getMenuData',
  cache: false,
  async: false,
  type: "POST",
  success: function (result) {
    JSON.parse(result).forEach(item => {
      let json = {
        path: "/api" + (item.href == "/" ? "" : item.href),
        component: HeaderAsideFooterResponsiveLayout,
        indexRoute: {
          component: Black
        }
      };

      if (undefined != item.children && item.children.length > 0) {
        let children = [];
        item.children.forEach(i => {
          let child = {
            path: i.href,
            component: Api,
            query: {
              action: item.href.substr(1, item.href.length),
              method: i.href,
              actionName: item.actionName
            }
          };
          children.push(child);
        });
        json["childRoutes"] = children;
      }

      autoGeneratedRoutes.push(json);
    }); //添加默认配置

    autoGeneratedRoutes.push({
      path: '/',
      childRoutes: [{
        path: '*',
        childRoutes: [],
        component: NotFound
      }],
      component: HeaderAsideFooterResponsiveLayout,
      indexRoute: {
        component: Black
      }
    });
  }
});
console.log(autoGeneratedRoutes);
// <!-- auto generated routes end -->

// 自定义路由，如果 path 相同则会覆盖自动生成部分的路由配置
const customRoutes = [];

export default (
    <Router
        history={hashHistory}
        routes={[...autoGeneratedRoutes, ...customRoutes]}
    />
);
